<?php


namespace app\admin\controller;

use app\common\model\Attrg as A;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class Attrg extends Base
{
    public function index(){
        $pid = Request::param('pid'); //类别pid
        $group_id = Request::param('group_id');
        $where = Request::param('where', []);
        $where = array_filter($where);
        $keyword = Request::param('keyword');
        $prefix = config('database.prefix');
        $list = A::where('group_id',$group_id)
            ->where(function ($query) use ($keyword) {
                if (!empty($keyword)) {
                    $query->whereLike('attrg_name', "%$keyword%");
                }
            })
            ->order('attrg_sort asc')
            ->paginate(10, false, ['query' => request()->param()]);
        //dump($list);die;
        $page = $list->render();
        $view = [
            'keyword' => $keyword,
            'page' => $page,
            'list' => $list,
            'pid' => $pid,
            'group_id'=>$group_id,
        ];
        View::assign($view);
        return View::fetch();
    }
    public function add(){
        $group_one = Db::name('attr')->find(['group_id'=>Request::param('group_id')]);
        //dump($group_one);die;
        if(empty($group_one)){
            $this->error('该筛选组id不存在');
        }
        if(Request::isPost()){
            $data = Request::post();
            $data['add_time'] = time();
            $data['add_user_id'] = Session::get('admin.admin_id');
            if (A::create($data) !==false) {
                $this->success('添加配置参数名称:' . $data['attrg_name'].'添加成功', 'index?group_id='.Request::param('group_id'));
            }else{
                $this->error('添加失败','index?group_id='.Request::param('group_id'));
            }
        }else{
            $view = [
                'group_id' => Request::param('group_id'),
                'attrg_one'      => null,
            ];
        }
        View::assign($view);
        return View::fetch();
    }
    public function edit(){
        $attrg_one = Db::name('attrg')->find(['attrg_id'=>Request::param('attrg_id')]);
        //dump($attrg_one);die;
        if (empty($attrg_one)) {
            $this->error('筛选组参数名称id不存在');
        }
        if(Request::isPost()){
            $data = Request::post();
            //dump($data);die;
            $data['add_time'] = time();
            $data['add_user_id'] = Session::get('admin.admin_id');
            $where['attrg_id'] = Request::param('attrg_id');
            if (A::update($data,$where) !==false) {
                $this->success('编辑配置参数名称:' . $data['attrg_name'].'编辑成功', 'index?group_id='.Request::param('group_id'));
            }else{
                $this->error('修改失败', 'index?group_id='.Request::param('group_id'));
            }
        }else{
            $view = [
                'group_id' => $attrg_one['group_id'],
                'attrg_one'=>$attrg_one,
            ];
        }
        View::assign($view);
        return View::fetch('add');
    }
    public function sort(){
        if (Request::isPost()) {
            $attrg_id = Request::param('attrg_id');
            $attrg_sort = Request::param('attrg_sort');
            //dump($attrg_sort);die;
            if(Db::name('attrg')->where(array('attrg_id'=>$attrg_id))->update(['attrg_sort' =>$attrg_sort])){
                $this->success('排序成功');
            }else{
                $this->error('没有改变参数');
            }
        }
    }

    public function del(){
        $attrg_id = Request::param('attrg_id');
        //dump($attrg_id);die;
        if (Db::name('attrg')->delete($attrg_id)){
            return success('删除筛选组参数成功！','/attrg/index?group_id='.Request::param('group_id'));
        }else{
            return error('删除筛选组参数失败！');
        }
    }

    public function pildel(){
        $id = Request::param('id');
        $group_id = Db::name('attrg')->where('attrg_id',$id)->field('group_id')->find();
        //dump($id);die;
        $ids = explode(',',$id);
        foreach ($ids as $k => $v){
            $res=Db::name('attrg')->delete($v);
        }
        if ($res){
            return success('批量删除筛选组参数成功！','/config/index?group_id=$group_id');
        }else{
            return error('批量删除筛选组参数失败！');
        }
    }

    public function ajaxConfigName (){
        $key = input('name');
        $value = input('param');
        $attrg_id = input('attrg_id', 0, 'intval');
        $attrg_info = new \app\common\model\Attrg();
        $res = $attrg_info->ajaxConfigName($value, $attrg_id);
        if ($res) {
            $data = array(
                'status' => 'n',
                'info' => '筛选组参数名称已经重复，请一个试试'
            );
        } else {
            $data = array(
                'status' => 'y',
                'info' => '验证通过'
            );
        }
        return json($data);
    }
}